package com.cn.wx.servlet;

import java.io.*;

public class ReadTest {
    public static void main(String[] args) {
        readOne();//每次读一个字节
//        readMore();
//        bufferedInputStream();
    }

    public static void bufferedInputStream() {
        String pathname = "D:\\111\\a\\b\\c\\d\\cc.txt";
        File file = new File(pathname);//报错，是因为我们File里面没有空的构造函数
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            byte[] bytes = new byte[10];//动态添加数组
            int a;
            while ((a = bufferedInputStream.read(bytes,0,bytes.length)) != -1) {
                System.out.println(new String(bytes, 0, a));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readMore() {
        String pathname = "D:\\111\\a\\b\\c\\d\\cc.txt";
        File file = new File(pathname);//报错，是因为我们File里面没有空的构造函数
        try {
            InputStream inputStream = new FileInputStream(file);//字节输入流
            int a;
            byte[] bytes = new byte[10];//动态添加数组
            while ((a = inputStream.read(bytes, 0, 5)) != -1) {
                System.out.println(new String(bytes, 0, a));
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readOne() {
        //空格，tab，字母，数字，都是一个字节，汉字，enter键是两个字节
//如果用字节流读数据，存在中文的话会出现乱码，
        String pathname = "D:\\111\\a\\b\\c\\d\\cc.txt";
        File file = new File(pathname);//报错，是因为我们File里面没有空的构造函数
        try {
            InputStream inputStream = new FileInputStream(file);//字节输入流
            int a;
            while ((a = inputStream.read()) != -1) {
                System.out.print((char) a);
            }
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
